# Maintainer: Caleb Maclennan <caleb@alerque.com>
# Contributor: Levente Polyak <anthraxx[at]archlinux[dot]org>
# Contributor: speps <speps at aur dot archlinux dot org>
# Contributor: Sébastien Luttringer

pkgbase=lgi
pkgname=(lua-lgi lua51-lgi lua53-lgi)
pkgver=0.9.2
pkgrel=10
pkgdesc='Lua bindings for gnome/gobject using gobject-introspection library'
url="https://github.com/pavouk/$pkgbase"
arch=(loong64 x86_64)
license=(MIT)
depends=(glibc glib2 libffi gobject-introspection-runtime)
makedepends=(gobject-introspection lua lua51 lua53)
_archive="$pkgbase-$pkgver"
source=("$url/archive/$pkgver/$_archive.tar.gz"
        "$pkgbase-lua54.patch::$url/pull/249.patch")
sha256sums=('cfc4105482b4730b3a40097c9d9e7e35c46df2fb255370bdeb2f45a886548c4f'
            'eaa5a39883bc76c77bea3fbf4ec0a8d60444bf78c6ff8e2096a550d30b986f29')

prepare() {
	cd "$_archive"
	patch -p1 < ../${source[1]%::*}

	cd ..
	cp -a "$_archive"{,-51}
	cp -a "$_archive"{,-53}
}

build() {
	CFLAGS="$CFLAGS -I/usr/include" make -C "$_archive" all
	CFLAGS="$CFLAGS -I/usr/include/lua5.1" make -C "$_archive-51" all
	CFLAGS="$CFLAGS -I/usr/include/lua5.3" make -C "$_archive-53" all
}

check() {
	cd "$_archive"
	# TODO: fix cairo-gobject dependency
	# make check
}

package_lua-lgi() {
	depends+=(lua)
	cd "$_archive"
	CFLAGS="$CFLAGS -I/usr/include" make \
		LUA_LIBDIR=/usr/lib/lua/5.4 \
		LUA_SHAREDIR=/usr/share/lua/5.4 \
		DESTDIR="$pkgdir/" install
	install -Dm0755 tools/dump-typelib.lua "$pkgdir/usr/bin/dump-typelib"
	install -Dm0644 -t "$pkgdir/usr/share/doc/$pkgname/" README.md docs/*
	install -Dm0644 -t "$pkgdir/usr/share/$pkgname/samples/" samples/*.lua
	install -Dm0644 -t "$pkgdir/usr/share/$pkgname/samples/gtk-demo/" samples/gtk-demo/*
	install -Dm0644 -t "$pkgdir/usr/share/licenses/$pkgname/" LICENSE
}

package_lua51-lgi() {
	depends+=(lua51)
	cd "$_archive-51"
	CFLAGS="$CFLAGS -I/usr/include/lua5.1" make \
		LUA_LIBDIR=/usr/lib/lua/5.1 \
		LUA_SHAREDIR=/usr/share/lua/5.1 \
		DESTDIR="$pkgdir/" install
	# install -Dm0755 tools/dump-typelib.lua "$pkgdir/usr/bin/dump-typelib"
	install -Dm0644 -t "$pkgdir/usr/share/doc/$pkgname/" README.md docs/*
	install -Dm0644 -t "$pkgdir/usr/share/$pkgname/samples/" samples/*.lua
	install -Dm0644 -t "$pkgdir/usr/share/$pkgname/samples/gtk-demo/" samples/gtk-demo/*
	install -Dm0644 -t "$pkgdir/usr/share/licenses/$pkgname/" LICENSE
}

package_lua53-lgi() {
	depends+=(lua53)
	cd "$_archive-53"
	CFLAGS="$CFLAGS -I/usr/include/lua5.3" make \
		LUA_LIBDIR=/usr/lib/lua/5.3 \
		LUA_SHAREDIR=/usr/share/lua/5.3 \
		DESTDIR="$pkgdir/" install
	# install -Dm0755 tools/dump-typelib.lua "$pkgdir/usr/bin/dump-typelib"
	install -Dm0644 -t "$pkgdir/usr/share/doc/$pkgname/" README.md docs/*
	install -Dm0644 -t "$pkgdir/usr/share/$pkgname/samples/" samples/*.lua
	install -Dm0644 -t "$pkgdir/usr/share/$pkgname/samples/gtk-demo/" samples/gtk-demo/*
	install -Dm0644 -t "$pkgdir/usr/share/licenses/$pkgname/" LICENSE
}
